import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 * 路由控制页面
 */
export const constantRoutes = [
  {
    path: '/login', // 登录
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404', // 404
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/', // 首页
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: '后台管理系统',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '后台管理系统', icon: 'dashboard' }
    }]
  },

  {
    path: '/blog',
    component: Layout,
    redirect: '/blog/blog',
    name: 'blogManager',
    meta: { title: '博客管理', icon: 'example' },
    children: [
      {
        path: 'type',
        name: 'type',
        component: () => import('@/views/blog/type/type-list'), // 路径
        meta: { title: '分类管理', icon: 'table' }
      },
      {
        path: 'blog',
        name: 'blog',
        component: () => import('@/views/blog/blog/blog-list'),
        meta: { title: '博客管理', icon: 'tree' }
      }
    ]
  },
  {
    path: '/sys',
    component: Layout,
    redirect: '/sys/sys',
    name: 'sysManager',
    meta: { title: '系统管理', icon: 'example' },
    children: [
      {
        path: 'user',
        name: 'user',
        component: () => import('@/views/sys/user/user-list'), // 路径
        meta: { title: '用户管理', icon: 'table' }
      },
      {
        path: 'admin',
        name: 'admin',
        component: () => import('@/views/sys/admin/admin-list'), // 路径
        meta: { title: '管理员管理', icon: 'table' }
      },
      {
        path: 'log',
        name: 'log',
        component: () => import('@/views/sys/log/log-list'),
        meta: { title: '日志管理', icon: 'tree' }
      }
    ]
  },
  {
    path: '/about', // 关于管理
    component: Layout,
    redirect: '/about',
    children: [{
      path: 'about',
      name: '关于管理',
      component: () => import('@/views/about/about-list'),
      meta: { title: '关于管理', icon: 'dashboard' }
    }]
  },
  {
    path: '/music', // 音乐管理
    component: Layout,
    redirect: '/music',
    children: [{
      path: 'music',
      name: '音乐管理',
      component: () => import('@/views/music/music-list'),
      meta: { title: '音乐管理', icon: 'dashboard' }
    }]
  },
  {
    path: '/link', // 友情链接
    component: Layout,
    redirect: '/link',
    children: [{
      path: 'link',
      name: '友情链接',
      component: () => import('@/views/link/link-list'),
      meta: { title: '友情链接', icon: 'dashboard' }
    }]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
